<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title></title>
		<meta name="renderer" content="webkit">
		<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
		<meta name="viewport"
			content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
		<link rel="stylesheet" href="../../../layuiadmin/layui/css/layui.css" media="all">
		<link rel="stylesheet" href="../../../layuiadmin/style/admin.css" media="all">
		<link rel="stylesheet" href="../../../css/common.css" media="all">
		<style type="text/css">
			.content {
				width: 80%;
				height: 350px;
				overflow-y: scroll;
				padding: 0 10px;
			}

			/*滚动条样式*/
			.content::-webkit-scrollbar {
				width: 4px;
				/*height: 4px;*/
			}

			.content::-webkit-scrollbar-thumb {
				border-radius: 10px;
				-webkit-box-shadow: inset 0 0 5px rgba(0, 0, 0, 0.2);
				background: rgba(0, 0, 0, 0.2);
			}

			.content::-webkit-scrollbar-track {
				-webkit-box-shadow: inset 0 0 5px rgba(0, 0, 0, 0.2);
				border-radius: 0;
				background: rgba(0, 0, 0, 0.1);

			}

			h3,
			.h3 {
				font-size: 12px;
				margin: 5px 0px;
				font-weight: 400;
			}

			.layui-timeline-item {
				position: relative;
				padding-bottom: 10px;
			}

			.layui-timeline-title {
				position: relative;
				margin-bottom: 0px;
			}

			.layui-text {
				line-height: 22px;
				font-size: 9px;
				color: #666;
			}

			.layui-text-notes {
				line-height: 22px;
				font-size: 15px;
				color: #f00;
			}

			.layui-badge.layui-bg-green span {
				color: #FFF !important;
			}

			.layui-form-label {
				width: 120px;
			}
			.layui-table-cell {
				height: auto;
				line-height: unset;
			}
		</style>
	</head>
	<body>

		<div class="layui-fluid reporting_style">
			<div class="layui-row" id="views">
				<!--<button type="button" class="layui-btn layui-btn-normal layui-btn-sm return">
					<i class="layui-icon layui-icon-return"></i>返回列表
				</button>-->
				<fieldset class="layui-elem-field layui-field-title" style="margin-top: 20px;">
					<legend>订单详情</legend>
				</fieldset>
				<div class="layui-card">
					<div class="layui-card-body">
						<div class="layui-row layui-col-space15" id="detail_view"></div>
						<div class="layui-row" id="view"></div>
						<div class="layui-fluid">
							<div class="layui-row" id="view">
								<h3>商品信息</h3>
								<table class="layui-hide" id="test-table-operate" lay-filter="test-table-operate">
								</table>
							</div>
						</div>
						<div class="layui-row" id="formView">
							<h3>退货信息</h3>
							<form class="layui-form" action="" style="width: 500px;">
								<div class="layui-form-item">
									<label class="layui-form-label">订单金额：</label>
									<div class="layui-input-inline">
										<div class="layui-form-mid layui-word-aux" id="formAmount" style="color: red !important;"></div>
									</div>
								</div>
								<div class="layui-form-item">
									<label class="layui-form-label">付款金额：</label>
									<div class="layui-input-inline">
										<div class="layui-form-mid layui-word-aux" id="formPayPrice" style="color: red !important;"></div>
									</div>
								</div>

								<div class="layui-form-item">
									<label class="layui-form-label">可退金额：</label>
									<div class="layui-input-inline">
										<div class="layui-form-mid layui-word-aux" id="formCanReturnPrice" style="color: red !important;"></div>
									</div>
								</div>

								<div class="layui-form-item">
									<label class="layui-form-label">应退货金额：</label>
									<div class="layui-input-inline">
										<div class="layui-form-mid layui-word-aux" id="formNeedReturnMoney" style="color: red !important;">请选择退货商品</div>
									</div>
								</div>

								<div class="layui-form-item">
									<label class="layui-form-label required">实退货金额：</label>
									<div class="layui-input-inline">
										<input id="formTrueReturnMoney" type="text" name="formTrueReturnMoney" required lay-verify="required" placeholder="实退货金额应小于等于应退货金额" autocomplete="off" class="layui-input">
									</div>
								</div>

								<div class="layui-form-item">
									<label class="layui-form-label">退货数量：</label>
									<div class="layui-input-inline">
										<div class="layui-form-mid layui-word-aux" id="formCanReturnQuantity" style="color: red !important;">请选择退货商品</div>
									</div>
								</div>

								<div class="layui-form-item">
									<div class="layui-input-block">
										<button class="layui-btn" lay-submit lay-filter="*">立即提交</button>
									</div>
								</div>
							</form>
						</div>
					</div>
				</div>
			</div>
		</div>

		<script type="text/html" id="zizeng">
		{{d.LAY_TABLE_INDEX+1}}
		</script>
		<script type="text/html" id="demo">
			<div class="layui-card">
				<!--<button type="button" class="layui-btn layui-btn-normal layui-btn-sm return">
					<i class="layui-icon layui-icon-return"></i>返回列表
				</button>-->
				<div class="layui-card-body">
					<div class="layui-row details">
						<div class="layui-col-xs3">
							订单状态：<span> {{ String(d.data.status) ? order_status_show(1,d.data.status) : '' }}<span>
						</div>
						<div class="layui-col-xs3">
							订单编号：<span> {{ d.data.orderid ? d.data.orderid : '' }}<span>
						</div>
						<div class="layui-col-xs3">
							会员编号：<span> {{ d.data.memberid ? d.data.memberid : '' }}<span>
						</div>


						<div class="layui-col-xs3">
							购买数量： <span> {{ d.data.quantity ? d.data.quantity : '' }}<span>
						</div>
						<div class="layui-col-xs3">
							订单金额： <span> {{ d.data.amount ? d.data.amount : '' }}<span>
						</div>
						<div class="layui-col-xs3">
							使用红包： <span> {{ d.data.fee_packet ? d.data.fee_packet : '' }}<span>
						</div>
						<div class="layui-col-xs3">
							使用积分： <span> {{ d.data.fee_score ? d.data.fee_score : '' }}<span>
						</div>
						<div class="layui-col-xs3">
							专属折扣： <span> {{ d.data.discount?d.data.discount:'' }}<span>
						</div>
						<div class="layui-col-xs3">
							付款金额： <span> {{d.data.pay_price?d.data.pay_price:''}}<span>
						</div>
						<div class="layui-col-xs3">
							付款方式：{{d.data.payment ? '<span class="text_red_color">' + payment_show(1, d.data.payment) + '</span>' : '未付款'}}
						</div>

						<div class="layui-col-xs3">
							下单时间： <span> {{ d.data.addtime ? d.data.addtime : ''}}<span>
						</div>
						<div class="layui-col-xs3">
							付款时间： <span> {{ d.data.pay_time ? d.data.pay_time : ''}}<span>
						</div>

					</div>
				</div>
			</div>
			<div class="layui-card">
				<div class="layui-card-body">
					<div class="layui-row details">
						<div class="layui-col-xs3">
							收货人姓名：<span>{{ d.data.consignee ? d.data.consignee : '' }}<span>
						</div>
						<div class="layui-col-xs3">
							收货人电话：<span>{{ d.data.mobile ? d.data.mobile : '' }}<span>
						</div>
						<div class="layui-col-xs6">
							收货地址：<span>{{ d.data.areaname ? d.data.areaname : '' }}{{ d.data.address ? d.data.address : '' }}<span>
						</div>

						{{# if(d.data.delivery_name){ }}
						<div class="layui-col-xs3">
							快递公司：<span>{{ d.data.delivery_name ? d.data.delivery_name : '' }}<span>
						</div>
						{{# }  }}
						{{# if(d.data.delivery_waybill){ }}
						<div class="layui-col-xs3">
							快递单号：<span>{{ d.data.delivery_waybill ? d.data.delivery_waybill : '' }}<span>
						</div>
						{{# }  }}
					</div>
				</div>
			</div>
		</script>

		<script src="../../../layuiadmin/layui/layui.js"></script>
		<script src="../../../js/jquery.min.js"></script>
		<script src="../../../js/common.js"></script>
		<script>
			var _data = {
				id: getParam('id'),
				orderid: getParam('orderid'),
				no: '',
				com: ''
			}

			var resData = {}, postData = {orderid: '', goods_return_money: '', status_goods_return: '', orderSubData: []}

			layui.config({
				base: '../../../layuiadmin/' //静态资源所在路径
			}).extend({
				index: 'lib/index' //主入口模块
			}).use(['index', 'form', 'laytpl', 'table'], function() {
				var $ = layui.$,
					admin = layui.admin,
					laytpl = layui.laytpl,
					table = layui.table,
					form = layui.form;

				initdata();

				function initdata() {
					$.get(server.queryordersreturndetail, _data, function(res, status) {
						if (status == 'success') {
							if (res.code == 0) {
								postData.orderid = res.orders.data.orderid
								resData = res.orders.data
								var data = res.orders
								var getTpl = demo.innerHTML,
									view = document.getElementById('view');
								laytpl(getTpl).render(data, function(html) {
									view.innerHTML = html;
								});
								$('#formAmount').text(resData.amount)
								$('#formPayPrice').text(resData.pay_price)
								var goods_return_money = resData.goods_return_money || 0
								console.log(goods_return_money);
								console.log(resData.pay_price);
								$('#formCanReturnPrice').text(resData.pay_price - goods_return_money)
								$.each(data.data.orders_sub, function(index, item) {
									item['canReturnQuantity'] = item.quantity - item.used_quantity - item.goods_return_quantity
								})
								initdata_list(data.data.orders_sub)
							} else {
								layer.msg(res.msg);
							}
						} else {
							layer.msg('服务器连接错误');
						}
					});
				}

				function initdata_list(tableData) {
					table.render({
						elem: '#test-table-operate',
						cols: [
							[{
									width: 50,
									field: 'zizeng',
									title: '#',
									fixed: 'left',
									templet: '#zizeng'
								}, {
									type: 'checkbox',
									fixed: 'left'
								}, {
									field: 'canReturnQuantity',
									fixed: 'left',
									width: 120,
									title: '可退货数量',
									edit: 'number'
								}, {
									field: 'memberid',
									width: 120,
									title: '会员编号'
								}, {
									field: 'userid',
									width: 120,
									title: '用户编号',
									sort: true
								}, {
									field: 'skuid',
									width: 120,
									title: '商品编号',
									sort: true
								}, {
									field: 'skuname',
									width: 160,
									title: '商品名称'
								},
								{
									field: 'specname',
									width: 120,
									title: '规格名称',
									sort: true,
								}, {
									field: 'specparam',
									width: 120,
									title: '规格参数',
									sort: true,
								}, {
									field: 'price_member',
									width: 120,
									title: '会员价格',
								}, {
									field: 'current_price',
									width: 120,
									title: '购买价格',
								},
								{
									field: 'score',
									width: 120,
									title: '积分可抵扣'
								}, {
									field: 'quantity',
									width: 160,
									title: '商品数量'
								}, {
									field: 'amount_price',
									width: 160,
									title: '商品总价'
								}, {
									field: 'price_waybill',
									width: 120,
									title: '订单运费',
								}, {
									field: 'mobile',
									width: 120,
									title: '手机号',
								}, {
									field: 'add_by',
									width: 120,
									title: '创建人',
								}
							]
						],
						data: tableData,
						done: function (res) {
							var state = "";
							for (var i in res.data) {
								var item = res.data[i];
								if(item.status_goods_return == "2") {// 这里是判断需要禁用的条件（如：状态为0的）
									// checkbox 根据条件设置不可选中
									$('tr[data-index=' + i + '] input[type="checkbox"]').prop('disabled', true);
									state = "1";// 隐藏表头全选判断状态
								}
							}
							//判断条件
							if (state == "1") {
								// 如果当前页有审核的不允许全选 checkbox
								$('th[data-field=1] div input[type="checkbox"]').prop('disabled', true);
							}
							form.render();// 重新渲染一下
						}
					});
				}

				form.on('submit(*)', function(data){
					var formTrueReturnMoney = parseFloat(data.field.formTrueReturnMoney)
					var formCanReturnPrice = parseFloat($('#formCanReturnPrice').text())
					console.log(formTrueReturnMoney + '||' + formCanReturnPrice);
					
					if (formTrueReturnMoney > formCanReturnPrice) {
						layer.msg('实退金额大于可退金额')
						return false;
					}
					var allCheckDatas = table.checkStatus('test-table-operate').data
					if (allCheckDatas.length <= 0) {
						layer.msg('请至少选择一个退货商品')
						return false;
					}
					postData.orderSubData = []
					var errMsg = '', currentReturnQuantity = 0
					$.each(allCheckDatas, function(index, item) {
						var canReturnQuantity = item.quantity - item.used_quantity - item.goods_return_quantity
						if (parseInt(item.canReturnQuantity) > canReturnQuantity) {
							errMsg = '编号[' + item.skuid + ']的商品超过可退货数量';
						}
						if (parseInt(item.canReturnQuantity) <= 0) {
							errMsg = '编号[' + item.skuid + ']的商品可退货数量不正确';
						}
						currentReturnQuantity += parseInt(item.canReturnQuantity)
						var itemStatus = parseInt(item.canReturnQuantity) == canReturnQuantity ? 2 : 1
						postData.orderSubData.push({
							id: item.id,
							goods_return_quantity: item.canReturnQuantity,
							status_goods_return: itemStatus
						})
					})
					if (errMsg !== '') {
						layer.msg(errMsg)
						return false;
					}
					var havReturnQuantity = 0, totalCanReturnQuantity = 0
					$.each(resData.orders_sub, function(index, item) {
						havReturnQuantity += item.goods_return_quantity
						totalCanReturnQuantity += (item.quantity - item.used_quantity)
					})
					// 总可退退货数量=已退货数量+本次退货数量。相等认为全部退货，否则认为部分退货。
					if (totalCanReturnQuantity === (havReturnQuantity + currentReturnQuantity)) {
						postData.status_goods_return = 2
					} else {
						postData.status_goods_return = 1
					}
					postData.goods_return_money = formTrueReturnMoney
					console.log(postData)
					var lock = false
					layer.confirm('确定要提交数据？', function() {
						if (!lock) {
							lock = true
							submitData();
						}
					});
					return false; //阻止表单跳转。如果需要表单跳转，去掉这段即可。
				});

				function submitData() {
					// 先更新子表
					loading();
					$.ajaxSettings.async = false;
					$.each(postData.orderSubData, function(index, item) {
						$.post(server.updateordersreturn_sub, item, function(res, status) {
							if (status == 'success') {
							} else {
							}
						});
					})
					$.ajaxSettings.async = true;
					$.post(server.updateordersreturn, postData, function(res, status) {
						if (status == 'success') {
							if (res.code == 0) {
								layer.alert('操作成功', function() {
									parent.layer.closeAll();
									parent.layui.table.reload('test-table-operate');
								});
							} else {
								closeloading();
								layer.msg(res.msg);
							}
						} else {
							layer.msg('服务器连接错误');
						}
					});
				}

				// 复选框事件
				table.on('checkbox(test-table-operate)', function(obj){
					var allCheckDatas = table.checkStatus('test-table-operate').data
					console.log(allCheckDatas);
					var formNeedReturnMoney = 0, formCanReturnQuantity = 0
					$.each(allCheckDatas, function(index, item) {
						formNeedReturnMoney += item.current_price
						formCanReturnQuantity += parseInt(item.canReturnQuantity)
					})
					$('#formNeedReturnMoney').text(formNeedReturnMoney == 0 ? '请选择退货商品' : formNeedReturnMoney)
					$('#formCanReturnQuantity').text(formCanReturnQuantity == 0 ? '请选择退货商品' : formCanReturnQuantity)
				});
			});
		</script>
	</body>
</html>
